#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        int i;
        int n = 0;
        sort(nums.begin(), nums.end());
        for (i = nums.size() - 1; i >= 2; i--) {
            int left = 0, right = i - 1;
            while (left < right) {
                if (nums[left] + nums[right] > nums[i]) {
                    n += right - left;
                    right--;
                }
                else {
                    left++;
                }
            }
        }
        return n;
    }
};